﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickFix;
using QuickFix42;

namespace Fix
{
    class Program
    {
        static void Main(string[] args) {
            QuickFix42.NewOrderSingle single = new NewOrderSingle( new ClOrdID( "id" ),
                                                                   new HandlInst( HandlInst.MANUAL ), 
                                                                   new Symbol( "IBM" ), 
                                                                   new Side( Side.BUY ),
                                                                   new TransactTime( DateTime.Now ),
                                                                   new OrdType( OrdType.LIMIT ) );
            single.getPrice().setValue( 100 );
            ExecutionReport report = new ExecutionReport();

            BindingSource account_binding = new BindingSource( report,
                                                               "Account",
                                                               () => report.getAccount().getValue() );


        }

        public class BindingSource
        {
            private Func<Object> m_GetterFunc;

            // weakreferences
            private Object m_GetterSource;

            private Object m_CachedValue;

            public Object Get
            {
                get
                {
                    if (m_CachedValue == null)
                    {
                        m_CachedValue = m_GetterFunc();
                    }
                    return m_CachedValue;
                }
            }

            public BindingSource( Object source_object, String name, Func<Object> getter ) {
                m_GetterFunc = delegate
                {
                    if (m_CachedValue == null)
                    {
                        m_CachedValue = getter();
                    }
                    return m_CachedValue;
                };
            }

            public BindingSource(Object source_object, String source_property)
            {
                Func<Object, Object> core_getter = delegate(object o) { return null; };
                m_GetterFunc = delegate
                {
                    if (m_CachedValue == null)
                    {
                        m_CachedValue = core_getter(m_GetterSource);
                    }
                    return m_CachedValue;
                };
            }
        }
    }
}
